package com.gitee.cashzhang27.test.cloud.seata.order.service;

import com.gitee.cashzhang27.test.cloud.seata.order.entity.OrderTbl;
import com.gitee.cashzhang27.test.cloud.seata.order.feign.UserFeignClient;
import com.gitee.cashzhang27.test.cloud.seata.order.mapper.OrderTblMapper;
import java.math.BigDecimal;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Description：
 *
 * @author fangliangsheng
 * @date 2019-04-04
 */
@Service
@AllArgsConstructor
public class OrderService {

  private UserFeignClient userFeignClient;
  private OrderTblMapper orderDAO;

  @Transactional
  public void create(String userId, String commodityCode, Integer count) {

    BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));

    OrderTbl order = new OrderTbl();
    order.setUserId(userId);
    order.setCommodityCode(commodityCode);
    order.setCount(count);
    order.setMoney(orderMoney);

    orderDAO.insert(order);

    userFeignClient.debit(userId, orderMoney);

  }

}
